package com.gcl.usercenter.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gcl.usercenter.common.BaseResponse;
import com.gcl.usercenter.common.ErrorCode;
import com.gcl.usercenter.common.ResultUtils;
import com.gcl.usercenter.model.User;
import com.gcl.usercenter.model.request.UserLoginRequest;
import com.gcl.usercenter.model.request.UserRegisterRequest;
import com.gcl.usercenter.service.UserService;
//import jakarta.annotation.Resource;
//import jakarta.servlet.http.HttpServletRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import static com.gcl.usercenter.service.impl.UserServiceImpl.USER_LOGIN_STATE;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

//import javax.annotation.Resource;

/**
 *
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private UserService userService;

    @PostMapping("/register")
    //浏览器默认是Get请求
    private BaseResponse<Long> UserRegister(@RequestBody UserRegisterRequest registerRequest) {
        if (registerRequest == null) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = registerRequest.getUserAccount();
        String userPassword = registerRequest.getUserPassword();
        String checkPassword = registerRequest.getCheckPassword();
        if (StringUtils.isAllBlank(userAccount, userPassword, checkPassword)) {
            return ResultUtils.error(ErrorCode.NULL_ERROR);
        }
        long userId = userService.register(userAccount, userPassword, checkPassword);
        return ResultUtils.success(userId);
    }

    @PostMapping("/login")
    private BaseResponse<User> UserLogin(@RequestBody UserLoginRequest loginRequest, HttpServletRequest request) {
        if (loginRequest == null) {
            return ResultUtils.error(ErrorCode.PARAMS_ERROR);
        }
        String userAccount = loginRequest.getUserAccount();
        String userPassword = loginRequest.getUserPassword();
        if (StringUtils.isAllBlank(userAccount, userPassword)) {
            return ResultUtils.error(ErrorCode.NULL_ERROR);
        }
        User user = userService.login(userAccount, userPassword, request);
        return ResultUtils.success(user);
    }

    //?
    @PostMapping("/search")
    private BaseResponse<List<User>> SearchUser(String username, HttpServletRequest request) {
        Object attribute = request.getSession().getAttribute(USER_LOGIN_STATE);
        User currentUser = (User) attribute;
        int userRole = currentUser.getUserRole();
        //鉴权，只有管理员可以搜索用户
        if (userRole != 1) {
            return ResultUtils.error(ErrorCode.NO_AUTH_Error);
        }
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("username", username);
        List<User> userList = userService.list(queryWrapper);
        List<User> list = userList.stream().map(user -> userService.getSafetyUser(user)).collect(Collectors.toList());
        return ResultUtils.success(list);
    }


    @GetMapping("/current")
    public BaseResponse<User> GetCurrentUser(HttpServletRequest request) {
        Object attribute = request.getSession().getAttribute(USER_LOGIN_STATE);
        User current = (User) attribute;
        if (current == null) {
            return ResultUtils.error(ErrorCode.NOT_LOGIN_ERROR);
        }
        //更新用户
        long userId = current.getId();
        User user = userService.getById(userId);
        User currentUser = userService.getSafetyUser(user);
        return ResultUtils.success(currentUser);
    }

    @PostMapping("/logout")
    public BaseResponse<Integer> UserLogout(HttpServletRequest request) {
        int userId = userService.uerLogout(request);
        return ResultUtils.success(userId);
    }

}
